<div>
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Blockers Configuration</h1>
        </div>
    </div>

    <span us-spinner="{radius:30, width:8, length: 16}" spinner-key="blocker-spinner"></span>
    <div class="row">
        <div class="col-lg-12 col-sm-12">
            <div class="panel panel-default">
                <div class="panel-heading apollo-form-headline">
                    Select Blocker
                    <button type="button" class="btn btn-success btn-xs pull-right" data-toggle="modal" data-target="#add-edit-blocker" ng-click="newBlocker()">
                        <i class="fa fa-plus"></i> New Blocker
                    </button>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-hover" datatable="ng" dt-options="dtOptions">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Service</th>
                                <th>Environment</th>
                                <th>Stack</th>
                                <th>Availability</th>
                                <th>Started</th>
                                <th>Active</th>
                                <th>Type</th>
                                <th>Configuration</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr ng-repeat="blocker in allBlockers">
                                <td>{{blocker.name}}</td>
                                <td>{{allServices[blocker.serviceId].name || "*"}}</td>
                                <td>{{allEnvironments[blocker.environmentId].name || "*"}}</td>
                                <td>{{allStacks[blocker.stackId].name || "*"}}</td>
                                <td>{{allBlockersAvailabilitiesList[blocker.id] || "*"}}</td>
                                <td>{{blocker.startedAt | date:'dd/MM/yy HH:mm:ss'}}</td>
                                <td>{{blocker.active}}</td>
                                <td>{{blocker.blockerTypeName}}</td>
                                <td>{{blocker.blockerJsonConfiguration}}</td>
                                <td>
                                    <button type="button" class="btn btn-primary btn-circle" uib-tooltip="Edit"
                                        ng-click="setCurrentBlocker(blocker)"
                                        data-toggle="modal" data-target="#add-edit-blocker">
                                        <i class="fa fa-pencil-square-o"></i>
                                    </button>
                                    <button type="button" class="btn btn-danger btn-circle" uib-tooltip="Delete"
                                        ng-click="deleteBlocker(blocker)">
                                        <i class="fa fa-trash-o"></i>
                                    </button>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="add-edit-blocker" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Add or edit blocker</h4>
                </div>
                <div class="modal-body">
                    <form name="blockerForm">
                        <h4>General Blocker Configuration</h4>
                        <div class="row">
                            <div class="col-lg-3 text-center" >
                                <span class="apollo-form-headline">Service</span> <br>
                                <select class="form-control apollo-filter" ng-model="blockerService" ng-options="service.name for service in allServices">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Environment</span> <br>
                                <select class="form-control apollo-filter" ng-model="blockerEnvironment" ng-options="environment.name for environment in allEnvironments">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Availability</span> <br>
                                <select class="form-control apollo-filter" ng-model="blockerAvailability" ng-options="availability for availability in allAvailabilitiesWithoutDuplicates">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Stack</span> <br>
                                <select class="form-control apollo-filter" ng-model="blockerStack" ng-options="stack.name for stack in allStacks">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Name</span> <br>
                                <div class="form control" ng-class="{'has-error': blockerForm.blockerNameInput.$error.pattern}">
                                    <input class="form-control" type="text" placeholder="blocker-name" name="blockerNameInput"
                                           ng-model="blockerName" ng-required="true" ng-pattern="/^[a-zA-Z0-9-_]*$/">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" ng-model="blockerActive"><strong>Active</strong>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <br><br>
                        <h4>Blocker Specific Configuration</h4>

                        <uib-tabset active="blockerTypeNameIndex">
                            <uib-tab index="0" heading="Unconditional Blocker" select="setBlockerTypeName('unconditional')">
                                <div>
                                    <h5>This blocker is unconditional, and will block everything as long as its there</h5>
                                </div>
                            </uib-tab>
                            <uib-tab index="1" heading="Time based Blocker" select="setBlockerTypeName('timebased')">
                                <div>
                                    <h5>This is a time based blocker, that will block deployments during the specified hours and days of the week</h5>
                                    <br>
                                    <div class="row">
                                        <div class="col-lg-3">
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    Start hour (UTC)
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <uib-timepicker ng-model="timeBasedBlockerStartTime.date" show-meridian="false"></uib-timepicker>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-lg-3">
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    End hour (UTC)
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <uib-timepicker ng-model="timeBasedBlockerEndTime.date" show-meridian="false"></uib-timepicker>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-lg-12">
                                            Days of the week <br>
                                            <label class="checkbox-inline" ng-repeat="day in days">
                                                <input type="checkbox" name="selectedDay[]"
                                                       value="{{day}}" ng-checked="selectedDays.indexOf(day) > -1"
                                                       ng-click="toggleSelectedDay(day)"> {{day}}
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </uib-tab>
                            <uib-tab index="2" heading="Branch Blocker" select="setBlockerTypeName('branch')">
                                <div>
                                    <h5>This blocker will only allow deployments of commits that exits in this specific branch history</h5>
                                    <br>
                                     <div class="row">
                                        <div class="col-lg-6">
                                            <div class="col-lg-4">
                                                Branch Name
                                            </div>
                                            <div class="col-lg-8">
                                                <div class="form control" ng-class="{'has-error': blockerForm.branchBlockerNameInput.$error.pattern}">
                                                    <input class="form-control" type="text" name="branchBlockerNameInput"
                                                           ng-model="branchBlockerName" ng-required="true" ng-pattern="/^[a-zA-Z0-9-_/]*$/"
                                                           placeholder="master / develop / .." ng-change="setBranchBlockerName(branchBlockerName)">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </uib-tab>
                            <uib-tab index="3" heading="Single Region Blocker" select="setBlockerTypeName('singleRegion')">
                                <div>
                                    <h5>This blocker doesn’t allow deployment of specified services to multiple regions at once.</h5>
                                </div>
                            </uib-tab>
                        </uib-tabset>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" data-dismiss="modal" ng-click="saveBlocker()">Save</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</div>
